// 拦截器
import axios from 'axios';
import Vue from 'vue'
//获取token
function getToken() {
    //假设token存储与seesionStorage中
    return Vue.$cookies.get('token')
}

//创建axios实例
let service = axios.create({
    baseURL: process.env.BASE_API,
    timeout: 5000
})

//请求拦截
service.interceptors.request.use(
    config => {
        if (getToken()) {
            config.headers['token'] = getToken();
            config.headers['Content-Type'] = 'application/json;chartset=utf-8';
        }
        return config;
    },
    error => {
        Promise.reject(error);
    }
)

//响应拦截
service.interceptors.response.use(
    response => {
        let res = response.data;
        if (response.status) {//请求成功时
            return res
        } else {
            //各种请求状态的处理
            console.log('error', response)
            return Promise.reject('error')
        }
    },
    error => {
        return Promise.reject(error);
    }
)

export default service;